---
import fs from 'fs';
import path from 'path';
import { Code } from 'astro-expressive-code/components';
import { getAbsoluteFilePathForAstroFile } from '@utils/files';

interface Props {
  file?: string;
  catalog: {
    filePath: string;
  };
  title?: string;
  filePath: string;
  lang?: string;
}

const { file = 'schema.json', catalog, title, filePath, lang = 'json' } = Astro.props;

let code: string | null = null;

const absoluteFilePath = getAbsoluteFilePathForAstroFile(filePath, file);

const exists = fs.existsSync(absoluteFilePath);
if (exists) {
  code = fs.readFileSync(absoluteFilePath, 'utf-8');
}
---

{
  code ? (
    <div class="not-prose max-w-4xl overflow-x-auto">
      <Code code={code} title={title || file} lang={lang} />
    </div>
  ) : (
    <div class="italic">Tried to load schema from {path.join(catalog.filePath, file)}, but no schema can be found</div>
  )
}
